﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Bll;

namespace SalomonWebsite.UC
{
    public partial class Head : System.Web.UI.UserControl
    {
        string strCon = Global.Connection;
        protected int countRow = 0;
        protected int countPage = 0;

        DataTable dtRtn = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {   
                //一级分类
                GetFirstCategory(strCon);
            }
        }


        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <param name="strCon">链接字符串</param>
        /// <param name="catId">等级</param>
        protected DataTable CategoryByCatId(string strCon,int catId) 
        {
            return Bll.Category.MULTIPLE(strCon, catId);
        }

        /// <summary>
        /// 一级分类
        /// </summary>
        /// <param name="strCon">链接字符串</param>
        protected void GetFirstCategory(string strCon) 
        {
            DataTable dtFirst = CategoryByCatId(strCon, 0);  //一级分类

            this.repFirst.DataSource = dtFirst.DefaultView;
            this.repFirst.DataBind();

            this.rpMenuDetail.DataSource = dtFirst.DefaultView;
            this.rpMenuDetail.DataBind();
            
            dtFirst.Dispose();
        }

        protected void RP_Cat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            // Execute the following logic for Items and Alternating Items.
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                // 找到分类Repeater关联的数据项
                DataRowView rowv = (DataRowView)e.Item.DataItem;

                //子分类显示
                Label lblCatList = ((Label)e.Item.FindControl("LabCat"));
                lblCatList.Text = CatList(Convert.ToInt32(rowv["PKID"]));
                
                
                //分类广告
                Label lblCatAd = ((Label)e.Item.FindControl("AdImg"));
               
                DataTable dtAD = new DataTable();
                dtAD = Bll.CategoryAd.Single(Global.Connection, 2, Convert.ToInt32(rowv["PKID"]));
                if (dtAD.Rows.Count > 0)
                {
                    lblCatAd.Text = "<a class=\"MenuMea\" href=\"/Ad-2-" + Convert.ToInt32(dtAD.Rows[0]["PKID"]) + ".aspx\"><img src=\"" + Global.ImageWeb + dtAD.Rows[0]["ImgURL"].ToString() + "\" alt=\"" + dtAD.Rows[0]["AdTitle"].ToString() + "\" /></a>"
						        +"<h3 class=\"MenuMeaTitle\"><a href=\"/Ad-2-"+Convert.ToInt32(dtAD.Rows[0]["PKID"])+".aspx\">"+dtAD.Rows[0]["AdTitle"].ToString()+"</a></h3>"
						        +"<p class=\"MenuMeaTxt\"><a href=\"/Ad-2-"+Convert.ToInt32(dtAD.Rows[0]["PKID"])+".aspx\">"+dtAD.Rows[0]["Description"].ToString()+"</a></p>";

                    dtAD.Dispose();
                }
                else
                {
                    lblCatAd.Text = "";
                }
                
            }
        }

        protected string CatList(int txbPKID)
        {
            GetCatList(txbPKID);
            string str = "";
            if (dtRtn.Rows.Count > 0)
            {
                int n = 0;
                int tmp = 0;
                for (int i = 0; i < dtRtn.Rows.Count; i++)
                {

                    

                    if (Convert.ToInt16(dtRtn.Rows[i]["CatRank"]) == 2) // 2级分类
                    {
                        if (tmp == 3)
                        {
                            str += "</ul></div>";
                        }

                        if (n == 3)
                        {
                            str += "</div>";
                            n = 0;
                        }

                        if (n == 0)
                        {
                            str += "<div class=\"MenuCol\">";
                        }

                        if (Convert.ToInt32(dtRtn.Rows[i]["Num"]) != 0)
                        {
                            str += "<div><h3><a href=\"Cat-" + txbPKID + "-" + dtRtn.Rows[i]["PKID"].ToString() + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></h3>";
                            //str += "<div><h3><a href=\"ProCat.aspx?pkid=" + dtRtn.Rows[i]["PKID"].ToString() + "\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></h3>";
                        }
                        else
                        {
                            str += "<div><h3><a href=\"Ser-" + txbPKID + "-" + dtRtn.Rows[i]["PKID"].ToString() + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></h3></div>";
                            //str += "<div><h3><a href=\"ProSeries.aspx?pkid=" + dtRtn.Rows[i]["PKID"].ToString() + "\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></h3>";

                        }

                        n++;
                    }

                    if (Convert.ToInt16(dtRtn.Rows[i]["CatRank"]) == 3) // 3级分类
                    {
                        str += "<li><a href=\"Ser-" + txbPKID + "-" + dtRtn.Rows[i]["PKID"].ToString() + "-" + dtRtn.Rows[i]["CatCode"].ToString() + ".aspx\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></li>";
                        //str += "<li><a href=\"ProSeries.aspx?pkid=" + dtRtn.Rows[i]["PKID"].ToString() + "\">" + dtRtn.Rows[i]["CatName"].ToString() + "</a></li>";
                        if (i == dtRtn.Rows.Count - 1)
                        {
                            str += "</ul></div>";
                        }
                    }

                    if (Convert.ToInt32(dtRtn.Rows[i]["Num"]) != 0)
                    {
                        str += "<ul>";
                    }

                    tmp = Convert.ToInt16(dtRtn.Rows[i]["CatRank"]);

                }
                str += "</div>";
            }
            else
            {
                str += "<div class=\"MenuCol\">&nbsp</div>";
            }

            dtRtn.Dispose();

            return str;
        }



        protected void GetCatList(int ParID)
        {
            DataTable dt = Bll.Category.MULTIPLE(Global.Connection);
            if (dt.Rows.Count > 0)
            {
                dtRtn = new DataTable();
                // Add three column objects to the table.
                DataColumn dc1 = new DataColumn();
                dc1.DataType = System.Type.GetType("System.Int32");
                dc1.ColumnName = "PKID";
                //dc1.AutoIncrement = true;
                dtRtn.Columns.Add(dc1);

                DataColumn dc2 = new DataColumn();
                dc2.DataType = System.Type.GetType("System.String");
                dc2.ColumnName = "CatName";
                //dc2.AutoIncrement = true;
                dtRtn.Columns.Add(dc2);



                DataColumn dc3 = new DataColumn();
                dc3.DataType = System.Type.GetType("System.Int32");
                dc3.ColumnName = "ParID";
                //dc3.AutoIncrement = true;
                dtRtn.Columns.Add(dc3);

                DataColumn dc4 = new DataColumn();
                dc4.DataType = System.Type.GetType("System.Int32");
                dc4.ColumnName = "CatRank";
                //dc4.AutoIncrement = true;
                dtRtn.Columns.Add(dc4);

                DataColumn dc5 = new DataColumn();
                dc5.DataType = System.Type.GetType("System.Int32");
                dc5.ColumnName = "SortBy";
                //dc5.AutoIncrement = true;
                dtRtn.Columns.Add(dc5);

                DataColumn dc6 = new DataColumn();
                dc6.DataType = System.Type.GetType("System.Int32");
                dc6.ColumnName = "Num";
                //dc5.AutoIncrement = true;
                dtRtn.Columns.Add(dc6);


                DataColumn dc7 = new DataColumn();
                dc7.DataType = System.Type.GetType("System.String");
                dc7.ColumnName = "CatCode";
                //dc2.AutoIncrement = true;
                dtRtn.Columns.Add(dc7);

                GetCatData(ParID, dt, dtRtn);

            }
        }


        protected void GetCatData(int parID, DataTable dtCat, DataTable dtRtn)
        {
            DataRow[] drs = dtCat.Select(" [ParID] = " + parID);

            for (int i = 0; i < drs.Length; i++)
            {
                DataRow dr = dtRtn.NewRow();
                dr["PKID"] = Convert.ToInt32(drs[i]["PKID"]);
                dr["CatName"] = drs[i]["CatName"].ToString();
                dr["ParID"] = Convert.ToInt32(drs[i]["ParID"]);
                dr["CatRank"] = Convert.ToInt32(drs[i]["CatRank"]);
                dr["SortBy"] = Convert.ToInt32(drs[i]["SortBy"]);
                dr["Num"] = Convert.ToInt32(drs[i]["Num"]);
                dr["CatCode"] = drs[i]["CatCode"].ToString();
                dtRtn.Rows.Add(dr);
                GetCatData(Convert.ToInt32(drs[i]["PKID"]), dtCat, dtRtn);
            }
        }

        #region //绑定二级分类和三级分类
        //protected DataTable CreateTableNum()
        //{
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("Id", typeof(Int32));
        //    return dt;
        //}

        //protected void GetNum(string strCon)
        //{
        //    DataTable dtSecrod = Bll.Category.MULTIPLE(strCon, 1);   //二级分类
        //    DataTable dtNum = CreateTableNum();
        //    int id = 0;
        //    if (dtNum.Rows.Count % 2 == 0)
        //        id = dtNum.Rows.Count / 2;
        //    else
        //        id = dtNum.Rows.Count / 2 + 1;
        //    for (int i = 1; i <= id; i++)
        //    {
        //        DataRow newRow = dtNum.NewRow();
        //        newRow["Id"] = i;
        //        dtNum.Rows.Add(newRow);
        //    }
        //    this.repNum.DataSource = dtNum.DefaultView;
        //    this.repNum.DataBind();
        //    dtNum.Dispose();
        //}

        //protected void repNum_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        Repeater rep = (Repeater)e.Item.FindControl("repSecordCat");
        //        DataRowView drv = (DataRowView)e.Item.DataItem;
        //        int pageIndex = Convert.ToInt32(drv["Id"]);
        //        rep.DataSource = GetSecord_Search(strCon, 2, 2, pageIndex, out countRow, out countPage);
        //        rep.DataBind();
        //    }
        //}

        //protected DataTable GetSecord_Search(string strCon, int catId, int size, int index, out int countRow, out int countPage)
        //{
        //    DataTable dtSecord = Bll.Category.MULTIPLE(strCon, size, index, out countRow, out countPage, catId);
        //    return dtSecord;
        //}

        //protected DataTable GetThird_Search(string strCon, int size, int index, out int countRow, out int countPage, int PKID)
        //{
        //    DataTable dt = Bll.Category.MULTIPLE_Third(strCon, size, index, out countRow, out countPage, PKID);
        //    return dt;
        //}

        //protected void repSecordCat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        Repeater rep = (Repeater)e.Item.FindControl("repThirdCat");
        //        DataRowView drv = (DataRowView)e.Item.DataItem;
        //        int PKID = Convert.ToInt32(drv["PKID"]);
        //        rep.DataSource = GetThird_Search(strCon, 100, 1, out countRow, out countPage, PKID);
        //        rep.DataBind();
        //    }
        //}
        #endregion
    }

}